package com.android.topview.watermelon.account.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.android.topview.watermelon.account.entity.Account
import com.android.topview.watermelon.account.reposity.AccountRepository

class AccountViewModel : ViewModel() {

    internal val costListLiveData = MutableLiveData<List<Account>>()

    internal val incomeListLiveData = MutableLiveData<List<Account>>()

    internal fun addAccount(account: Account): Long {
        return AccountRepository.addAccount(account)
    }

    internal fun getIncomesLiveData() {
        val list = AccountRepository.getIncomes()
        if (list != incomeListLiveData.value) {
            incomeListLiveData.postValue(list)
        }
    }

    internal fun getCostsLiveData() {
        val list = AccountRepository.getCosts()
        if (list != costListLiveData.value) {
            costListLiveData.postValue(list)
        }
    }

    internal fun deleteAccount(account: Account) {
        AccountRepository.deleteAccount(account)
        checkIncomeOrCost(account)
    }

    private fun checkIncomeOrCost(account: Account) {
        if (account.isCost) {
            getCostsLiveData()
        } else {
            getIncomesLiveData()
        }
    }

}